package com.hdaccp.shuiguoD.service.impl;

import com.hdaccp.shuiguoD.dao.JinhuolsDao;
import com.hdaccp.shuiguoD.dao.XiaoshoulsDao;
import com.hdaccp.shuiguoD.dao.jinxiaoDao;
import com.hdaccp.shuiguoD.entity.Jinhuols;
import com.hdaccp.shuiguoD.entity.Xiaoshouls;
import com.hdaccp.shuiguoD.service.jinxiaoService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Service
public class jinxiaoServiceImpl implements jinxiaoService {

    @Autowired
    private jinxiaoDao jinxiaoDao;
    @Autowired
    private JinhuolsDao jinhuolsDao;
    @Autowired
    private XiaoshoulsDao xiaoshoulsDao;


    @Override
    public List<Jinhuols> jinhuo(String yi, String er) {
        return jinxiaoDao.jinhuo(yi,er);
    }

    @Override
    public List<Xiaoshouls> xiaoshou(String yi, String er) {
        return jinxiaoDao.xiaoshou(yi, er);
    }

    @Override
    public void ExportUserList(HttpServletResponse response) {
        //HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook workBook=new HSSFWorkbook();

        //sheet对象（excel的表单）
        HSSFSheet sheet=workBook.createSheet("进货表信息");

        //行数，参数为行索引(excel的行)
        HSSFRow rowHead=sheet.createRow(0);//第一行  可以是0～65535之间的任何一个

        //创建excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell cellOne=rowHead.createCell(0);
        cellOne.setCellValue("进货信息");//表头

        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,9));

        HSSFRow rows=sheet.createRow(1);//第二行
        //创建单元格并设置单元格内容
        rows.createCell(0).setCellValue("进货单号");
        rows.createCell(1).setCellValue("进货日期");
        rows.createCell(2).setCellValue("供应商");
        rows.createCell(3).setCellValue("经办人");
        rows.createCell(4).setCellValue("金额");



        List<Jinhuols>userList=jinhuolsDao.queryAll();
        for(int i=0;i<userList.size();i++){
            HSSFRow row=sheet.createRow(i+2);//从第三行开始
            //创建单元格并设置单元格内容
            row.createCell(0).setCellValue(userList.get(i).getDanhao());
            row.createCell(1).setCellValue(userList.get(i).getRiqi());
            row.createCell(2).setCellValue(userList.get(i).getGonghuo());
            row.createCell(3).setCellValue(userList.get(i).getJingban());
            row.createCell(4).setCellValue(userList.get(i).getShifu());

        }
        //输出Excel文件
        try {
            OutputStream output=response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=userinfo.xls");
            response.setContentType("application/msexcel");
            workBook.write(output);
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void ExportUserList1(HttpServletResponse response) {
        //HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook workBook=new HSSFWorkbook();

        //sheet对象（excel的表单）
        HSSFSheet sheet=workBook.createSheet("销售表信息");

        //行数，参数为行索引(excel的行)
        HSSFRow rowHead=sheet.createRow(0);//第一行  可以是0～65535之间的任何一个

        //创建excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell cellOne=rowHead.createCell(0);
        cellOne.setCellValue("销售信息");//表头

        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,9));

        HSSFRow rows=sheet.createRow(1);//第二行
        //创建单元格并设置单元格内容
        rows.createCell(0).setCellValue("销售单号");
        rows.createCell(1).setCellValue("销售日期");
        rows.createCell(2).setCellValue("顾客");
        rows.createCell(3).setCellValue("经办人");
        rows.createCell(4).setCellValue("金额");



        List<Xiaoshouls>userList=xiaoshoulsDao.queryAll();
        for(int i=0;i<userList.size();i++){
            HSSFRow row=sheet.createRow(i+2);//从第三行开始
            //创建单元格并设置单元格内容
            row.createCell(0).setCellValue(userList.get(i).getDanhao());
            row.createCell(1).setCellValue(userList.get(i).getRiqi());
            row.createCell(2).setCellValue(userList.get(i).getHname());
            row.createCell(3).setCellValue(userList.get(i).getJingban());
            row.createCell(4).setCellValue(userList.get(i).getShifu());

        }
        //输出Excel文件
        try {
            OutputStream output=response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=userinfo.xls");
            response.setContentType("application/msexcel");
            workBook.write(output);
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
